//
//  FRQAppDelegate.m
//  FRQBluetoothKit
//
//  Created by xuchunhai on 12/31/2020.
//  Copyright (c) 2020 xuchunhai. All rights reserved.
//

#import "FRQAppDelegate.h"
//#import <FRQBluetoothKit/FRQBluetoothManager.h>
#import "FRQLoginViewController.h"
#import "skJPUSHSet.h"
#import "FRQAppOpenViewController.h"
#import "FRQFeedbackViewController.h"


@implementation FRQAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor=[UIColor whiteColor];
    [[skJPUSHSet sharedskJPUSHSet] skJpushSet:launchOptions];
    [[[UIApplication sharedApplication] delegate] window].rootViewController=[[FRQAppOpenViewController alloc] init];
    [self.window makeKeyAndVisible];
    [application setApplicationIconBadgeNumber:0];
    NSLog(@"%%");
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    //蓝牙11.30
    [self createBlueNoti];
    // Override point for customization after application launch.
    [self skMangerKeyboard];
    //状态栏高度
    [self getStatusHeight];
    for(NSString *fontFamilyName in [UIFont familyNames]) {
        NSLog(@"fontFamilyName:'%@'",fontFamilyName);
        for(NSString *fontName in [UIFont fontNamesForFamilyName:fontFamilyName]) {
            NSLog(@"    font:'%@'",fontName);
        }
        NSLog(@"*****************************");
    }
    return YES;
}
-(void)skMangerKeyboard{
    IQKeyboardManager *keyboardManager = [IQKeyboardManager sharedManager]; // 获取类库的单例变量
    
    keyboardManager.enable = YES; // 控制整个功能是否启用
    
    keyboardManager.shouldResignOnTouchOutside = YES; // 控制点击背景是否收起键盘
    
    keyboardManager.shouldToolbarUsesTextFieldTintColor = YES; // 控制键盘上的工具条文字颜色是否用户自定义
    
    keyboardManager.toolbarManageBehaviour = IQAutoToolbarBySubviews; // 有多个输入框时，可以通过点击Toolbar 上的“前一个”“后一个”按钮来实现移动到不同的输入框
    
    keyboardManager.enableAutoToolbar = YES; // 控制是否显示键盘上的工具条
    
    keyboardManager.shouldShowToolbarPlaceholder = YES; // 是否显示占位文字
    
    keyboardManager.placeholderFont = [UIFont boldSystemFontOfSize:17]; // 设置占位文字的字体
    
    keyboardManager.keyboardDistanceFromTextField = 10.0f; // 输入框距离键盘的距离
    
    

}
//蓝牙11.30
- (void)createBlueNoti{
    //FRINotificationAtDidConnectPeripheral  蓝牙连接成功的通知
    //FRINotificationAtDidFailToConnectPeripheral  蓝牙连接失败
    //FRINotificationAtDidDisconnectPeripheral  蓝牙断开连接
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(connectSuccess:) name:@"FRINotificationAtDidConnectPeripheral" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(connectError:) name:@"FRINotificationAtDidFailToConnectPeripheral" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(connectClose:) name:@"FRINotificationAtDidDisconnectPeripheral" object:nil];
    
}
- (void)startTimer{
    self.count = 0;
    [self.timer invalidate];
    self.timer = nil;
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:900.0 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    
}

- (void)timerAction{
    self.count++;
    NSLog(@"-------%ld",self.count);
    [[FRQBluetoothManager shareManager] closeAllPeripheralsConnection:^(id<FRBleAbility>  _Nonnull ability) {
            
    }];
    [self.timer invalidate];
    self.timer = nil;
}
- (void)connectSuccess:(NSNotification *)noti{
//    [SkToast SkToastShow:@"连接蓝牙成功"];
    [FRQBluetoothManager shareManager].isConnect = 1;
    [self startTimer];
}
- (void)connectError:(NSNotification *)noti{
//    [SkToast SkToastShow:@"连接失败"];
    //蓝牙12.01
    [[NSNotificationCenter defaultCenter] postNotificationName:@"FRQCropped" object:nil];
    [FRQBluetoothManager shareManager].isConnect = 0;
}
- (void)connectClose:(NSNotification *)noti{
//    [SkToast SkToastShow:@"Connection disconnected"];
    [self.window makeToast:@"Connection disconnected" duration:2.0];
//    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"Connection disconnected" preferredStyle:UIAlertControllerStyleAlert];
//    UIAlertAction *action = [UIAlertAction actionWithTitle:@"sure" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//
//    }];
//    [alert addAction:action];
//    [self.window.rootViewController presentViewController:alert animated:YES completion:nil];
    //蓝牙12.01
    [FRQBluetoothManager shareManager].isConnect = 0;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"FRQCropped" object:nil];
}

- (void)getStatusHeight{
    if (@available(iOS 13.0, *)) {
        UIStatusBarManager *statusBarManager = [UIApplication sharedApplication].windows.firstObject.windowScene.statusBarManager;
        self.statusBarHeight = statusBarManager.statusBarFrame.size.height;
    }
    else {
        self.statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
    }
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    
    self.taskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"com.frq.background_bluetoothTask" expirationHandler:^{
        
        [[UIApplication sharedApplication] endBackgroundTask:self.taskIdentifier];
        self.taskIdentifier = UIBackgroundTaskInvalid;
    }];
    
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    [JPUSHService setBadge:0];
    [application setApplicationIconBadgeNumber:0];
    [[UIApplication sharedApplication] endBackgroundTask:self.taskIdentifier];
    self.taskIdentifier = UIBackgroundTaskInvalid;
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    
//    [[FRQBluetoothManager shareManager] closeAllPeripheralsConnection:^(id<FRBleAbility>  _Nonnull ability) {
//
//    }];
    
    
}

@end
